﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Brain.Windows;
using System.Reflection;
using Brain.Editor.Properties;

namespace Brain.Editor
{
    public partial class NewLevelForm : Form
    {
        public MainEditor Editor;

        public NewLevelForm(MainEditor editor)
        {
            this.Editor = editor;
            InitializeComponent();

            Panel panel = panel1;

            Label label = new Label();
            label.Text = "Selected the Game Folder to see " + Environment.NewLine + "the Game's Plugins";
            label.Location = new Point(10, 30);
            panel.Controls.Add(label);

            Test();
        }

        bool failedTest = false;
        private void Test()
        {
            if (string.IsNullOrEmpty(this.text_SaveLocation.Text) ||
                string.IsNullOrEmpty(this.text_LevelName.Text) ||
                File.Exists(Path.Combine(this.text_SaveLocation.Text, this.text_LevelName.Text)))
            {
                failedTest = true;
                gameFolderStatus.Image = Resources.inativo;
            }
            else
            {
                failedTest = false;
                gameFolderStatus.Image = Resources.ativo;
            }
        }
        private void text_LevelName_TextChanged(object sender, EventArgs e)
        {
            Test();
        }

        private void button_SaveLocation_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.RootFolder = Environment.SpecialFolder.MyDocuments;

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.text_SaveLocation.Text = dialog.SelectedPath;

                string rootFolder = this.text_SaveLocation.Text;
                string pluginsFolder = Path.Combine(rootFolder, "Plugins");
                if (Directory.Exists(pluginsFolder))
                {
                    DirectoryInfo dir = new DirectoryInfo(pluginsFolder);
                    if (dir.Exists)
                    {
                        FileInfo[] Assemblies = dir.GetFiles();
                        Engine.Instance.EditorManager.PluginManager.HandleAssemblies(rootFolder, Assemblies);
                    }
                }
                UpdatePanel();
            }
            Test();
        }

        private void UpdatePanel()
        {
            Panel panel = panel1;
            panel.Controls.Clear();
            for (int i = 0; i < Engine.Instance.EditorManager.PluginManager.PluginTypes.Count; i++)
            {
                LoadPlugin load = Engine.Instance.EditorManager.PluginManager.PluginTypes[i];
                Type t = load.PluginType;
                CheckBox box = new CheckBox();
                box.Text = t.Name;
                box.Tag = i;
                box.Size = new Size(300, 20);
                box.Location = new Point(10, 10 + (i * 25));
                panel.Controls.Add(box);
            }
            if (Engine.Instance.EditorManager.PluginManager.PluginTypes.Count == 0)
            {
                Label label = new Label();
                label.Text = "No plugins found!";
                label.Location = new Point(10, 30);
                panel.Controls.Add(label);
            }
        }

        private void button_OK_Click(object sender, EventArgs e)
        {
            if (!failedTest)
            {
                LevelData data = new LevelData();

                string levelName = this.text_LevelName.Text;
                string rootFolder = this.text_SaveLocation.Text;

                data.RootFolder = rootFolder;
                data.LevelName = levelName;

                string levelsFolder = Path.Combine(rootFolder, "Levels");
                string levelFile = Path.Combine(levelsFolder, levelName + ".brain");
                string contentFolder = Path.Combine(rootFolder, "Content");
                string tempFolder = Path.Combine(contentFolder, "Temp");
                string pluginsFolder = Path.Combine(rootFolder, "Plugins");

                if (!Directory.Exists(contentFolder))
                {
                    Directory.CreateDirectory(contentFolder);
                }
                if (!Directory.Exists(pluginsFolder))
                {
                    Directory.CreateDirectory(pluginsFolder);
                }
                if (!Directory.Exists(levelsFolder))
                {
                    Directory.CreateDirectory(levelsFolder);
                }
                if (!Directory.Exists(tempFolder))
                {
                    Directory.CreateDirectory(tempFolder);
                }

                FileStream s = File.Create(levelFile);
                s.Dispose();

                data.ContentProject = new ContentProject(levelName);

                if (Engine.Instance != null)
                {
                    Engine.EndAndDispose();
                }
                Engine.InitializeEngineWithDefaultManagers(GraphicsDeviceService.SingleInstance);
                Engine.Instance.Editor = this.Editor;
                Engine.Instance.SerializationManager.LevelData = data;

                GameScreen Screen = new GameScreen();

                string dataBaseFile = Path.Combine(Path.Combine(Engine.Instance.SerializationManager.LevelData.RootFolder, "Database.data"));
                if (File.Exists(dataBaseFile))
                {
                    using (FileStream stream = new FileStream(dataBaseFile, FileMode.Open))
                    {
                        DatabaseManager manager =
                            (DatabaseManager)Engine.Instance.SerializationManager.DeserializeUnique(stream, FileType.XML);
                        Engine.Instance.EditorManager.DatabaseManager = manager;
                    }
                }

                for (int i = 0; i < panel1.Controls.Count; i++)
                {
                    Control control = panel1.Controls[i];
                    if (control is CheckBox)
                    {
                        CheckBox box = control as CheckBox;
                        if (box.Checked)
                        {
                            int index = (int)box.Tag;
                            LoadPlugin load = Engine.Instance.EditorManager.PluginManager.PluginTypes[index];
                            Type t = load.PluginType;
                            IEditorPlugin plugin = (IEditorPlugin)Activator.CreateInstance(t);
                            plugin.Load();

                            data.LoadPlugins.Add(load);
                        }
                    }
                }

                Engine.Instance.EditorManager.FinishedInsertingTypes();

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }



       
    }
}
